package cn.edu.swu.design

import CountFragment
import HomeFragment
import MeFragment
import VideoFragment
import android.Manifest
import android.content.pm.PackageManager
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.KeyEvent
import android.view.View
import android.widget.RadioGroup
import android.widget.RelativeLayout
import android.widget.TextView
import android.widget.Toast
import androidx.annotation.RequiresApi
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.viewpager.widget.ViewPager
import cn.edu.swu.design.adapter.MyFragmentPagerAdapter

class MainActivity : AppCompatActivity() {

    private var viewPager: ViewPager? = null
    private var radioGroup: RadioGroup? = null

    @RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)
    private var tv_main_title: TextView? = null
    private var rl_title_bar: RelativeLayout? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        drawingAuthority()
        initView()
    }

    private fun drawingAuthority() {
        if (ContextCompat.checkSelfPermission(
                this@MainActivity,
                Manifest.permission.WRITE_EXTERNAL_STORAGE
            ) != PackageManager.PERMISSION_GRANTED
        ) {
            ActivityCompat.requestPermissions(
                this@MainActivity,
                arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE),
                1
            )
        }
    }

    private fun initView() {
        tv_main_title = findViewById<View>(R.id.tv_main_title) as TextView
        tv_main_title!!.text = "首页"
        rl_title_bar = findViewById<View>(R.id.title_bar) as RelativeLayout
        rl_title_bar!!.setBackgroundColor(resources.getColor(R.color.rdTextColorPress))
        radioGroup = findViewById<View>(R.id.radioGroup) as RadioGroup

        // RadioGroup选中状态改变监听
        radioGroup!!.setOnCheckedChangeListener { radioGroup, i ->
            when (i) {
                R.id.rb_home ->                         //setCurrentItem()方法中第二个参数控制页面切换动画，true:打开，false:关闭
                    viewPager!!.setCurrentItem(0, false)
                R.id.rb_count -> viewPager!!.setCurrentItem(1, false)
                R.id.rb_video -> viewPager!!.setCurrentItem(2, false)
                R.id.rb_me -> viewPager!!.setCurrentItem(3, false)
            }
        }
        viewPager = findViewById(R.id.viewPager)
//        val viewPager:ViewPager = findViewById(R.id.viewPager)

        val homeFragment = HomeFragment()
        val countFragment = CountFragment()
        val videoFragment = VideoFragment()
        val meFragment = MeFragment()
        val alFragment: MutableList<Fragment> = ArrayList()
        alFragment.add(homeFragment)
        alFragment.add(countFragment)
        alFragment.add(videoFragment)
        alFragment.add(meFragment)
        viewPager?.setOffscreenPageLimit(3) //三个界面之间来回切换都不会重新加载数据。
        viewPager?.setAdapter(MyFragmentPagerAdapter(supportFragmentManager, alFragment))
        viewPager?.setCurrentItem(0)
        // viewPager 页面切换监听
        viewPager?.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {
            }

            override fun onPageSelected(position: Int) {
                when (position) {
                    0 -> {
                        radioGroup!!.check(R.id.rb_home)
                        tv_main_title!!.text = "首页"
                        rl_title_bar!!.visibility = View.VISIBLE
                    }
                    1 -> {
                        radioGroup!!.check(R.id.rb_count)
                        tv_main_title!!.text = "统计"
                        rl_title_bar!!.visibility = View.VISIBLE
                    }
                    2 -> {
                        radioGroup!!.check(R.id.rb_video)
                        tv_main_title!!.text = "视频"
                        rl_title_bar!!.visibility = View.VISIBLE
                    }
                    3 -> {
                        radioGroup!!.check(R.id.rb_me)
                        rl_title_bar!!.visibility = View.GONE
                    }
                }
            }

            override fun onPageScrollStateChanged(state: Int) {}
        })
    }

    // 记录第一次点击时间
    protected var exitTime: Long = 0
    override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK
            && event.action == KeyEvent.ACTION_DOWN
        ) {
            if (System.currentTimeMillis() - exitTime > 2000) {
                Toast.makeText(
                    this@MainActivity, "再按一次退出黑马头条",
                    Toast.LENGTH_SHORT
                ).show()
                exitTime = System.currentTimeMillis()
            } else {
                finish()
                System.exit(0)
            }
            return true
        }
        return super.onKeyDown(keyCode, event)
    }
}